package com.sgcc.ln.pmos.common.utils;


import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sgcc.ln.pmos.common.widget.LoadingLayout;

import java.util.List;

public class SmartLoadUtil {
    /**
     * 通用支持加载更多
     *
     * @param list
     * @param hasNext
     * @param smartRefreshLayout
     * @param loadingLayout
     * @param isRefresh
     */
    public static void canLoadMore(List list, boolean hasNext, SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh) {
        canLoadMore(list, hasNext, smartRefreshLayout, loadingLayout, isRefresh, false);
    }

    public static void canLoadMore(List list, boolean hasNext, SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh, boolean isShowContet) {
        try {
            smartRefreshLayout.resetNoMoreData();
            if (list == null || list.size() == 0) {
                if (isShowContet) {
                    loadingLayout.showContent();
                } else {
                    loadingLayout.showEmpty();
                }
                smartRefreshLayout.setEnableLoadMore(false);//禁止加载
            } else if (hasNext) {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);//允许加载
            } else {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);
                if (list.size() <= 10) {
                    smartRefreshLayout.finishRefreshWithNoMoreData();
                } else {
                    smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            if (isRefresh) {
                smartRefreshLayout.finishRefresh();
            } else {
                smartRefreshLayout.finishLoadMore();
            }
        } catch (Exception e) {
        }
    }


    /**
     * 通用失败支持加载更多
     *
     * @param smartRefreshLayout
     * @param loadingLayout
     * @param isRefresh
     */
    public static void canFailLoadMore(SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh) {
        canFailLoadMore(smartRefreshLayout, loadingLayout, isRefresh, false);
    }

    public static void canFailLoadMore(SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh, boolean isShowContent) {

        try {
            if (isRefresh) {
                if (isShowContent) {
                    loadingLayout.showContent();
                } else {
                    loadingLayout.showError();
                }
                smartRefreshLayout.setEnableLoadMore(false);//禁止加载
                smartRefreshLayout.finishRefresh();
            } else {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);//允许加载
                smartRefreshLayout.finishLoadMore();
            }
        } catch (Exception e) {

        }
    }


    public static void canLoadMore(List list, List currentlist, SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh) {

        try {
            if (list == null || list.size() == 0) {
                loadingLayout.showEmpty();
                smartRefreshLayout.setEnableLoadMore(false);//禁止加载
            } else if (currentlist != null && currentlist.size() != 0) {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);//允许加载
            } else {
                loadingLayout.showContent();
                if (list.size() <= 10) {
                    smartRefreshLayout.finishRefreshWithNoMoreData();
                } else {
                    smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            if (isRefresh) {
                smartRefreshLayout.finishRefresh();
            } else {
                smartRefreshLayout.finishLoadMore();
            }
        } catch (Exception e) {

        }
    }

    /**
     * 禁止加载更多
     *
     * @param list
     * @param smartRefreshLayout
     * @param loadingLayout
     * @param isRefresh
     */
    public static void forbidLoadMore(List list, SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh) {
        forbidLoadMore(list, smartRefreshLayout, loadingLayout, isRefresh, false);
    }

    public static void forbidLoadMore(List list, SmartRefreshLayout smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh, boolean isShowContent) {

        try {
            if (list == null || list.size() == 0) {
                if (isShowContent) {
                    loadingLayout.showContent();
                } else {
                    loadingLayout.showEmpty();
                }
                smartRefreshLayout.setEnableLoadMore(false);//禁止加载
            } else {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);//允许加载
            }
            if (isRefresh) {
                smartRefreshLayout.finishRefreshWithNoMoreData();
            }
        } catch (Exception e) {

        }
    }


    /**
     * 禁止加载更多失败操作
     *
     * @param list
     * @param smartRefreshLayout
     * @param loadingLayout
     * @param isRefresh
     */
    public static void forbidFailLoadMore(List list, SmartRefreshLayout
            smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh) {
        forbidFailLoadMore(list,
                smartRefreshLayout, loadingLayout, isRefresh, false);
    }

    public static void forbidFailLoadMore(List list, SmartRefreshLayout
            smartRefreshLayout, LoadingLayout loadingLayout, boolean isRefresh, boolean isShowContent) {

        try {
            if (list == null || list.size() == 0) {
                if (isShowContent) {
                    loadingLayout.showContent();
                } else {
                    loadingLayout.showError();
                }
                smartRefreshLayout.setEnableLoadMore(false);//禁止加载
            } else {
                loadingLayout.showContent();
                smartRefreshLayout.setEnableLoadMore(true);//允许加载（才会显示没有更多）
            }
            if (isRefresh) {
                smartRefreshLayout.finishRefreshWithNoMoreData();
            }
        } catch (Exception e) {

        }
    }


}

